﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using KC.DataAccess.MV6;
using KC.ExtensionMethods;
using net = KC.Business.Net;

public partial class Stats : System.Web.UI.Page
{
	MV6DataContext dc = new MV6DataContext(ConfigurationManager.ConnectionStrings["MV6"].ConnectionString);
	protected void Page_Load(object sender, EventArgs e)
	{
		// Handle Monthly Hits
		using (DataTable dt = utils.GetMonthlyHits()) {
			dgHits.DataSource = dt;
			dgHits.DataBind();

			for (int i = 0; i < dt.Rows.Count; i++) {
			    cTrafficByMonth.Compression = 100;
			    cTrafficByMonth.Series["Spiders"].Points.AddXY(i, dt.Rows[i]["Spiders"].ToString());
			    cTrafficByMonth.Series["Humans"].Points.AddXY(i, dt.Rows[i]["Humans"].ToString());
			    cTrafficByMonth.Series["Total"].Points.AddXY(i, dt.Rows[i]["Total Hits"].ToString());
				cTrafficByMonth.Series[0].Points[i].AxisLabel = DateTime.Parse(dt.Rows[i][0].ToString()).ToString("MM/yy"); }
		}


		dgSearchStatus.DataSource = (from s in dc.SearchStatus
									 orderby s.Rank, s.Engine, s.LastChecked descending
									 select s).Take(11);
		dgSearchStatus.DataBind();

		dgSearches.DataSource = (from s2 in
									 (from s in dc.SearchResults
									  orderby s.ID descending
									  select new { s.ID, s.Engine, s.Query })
								 select new { s2.Engine, s2.Query }).Take(50);
		dgSearches.DataBind();

		dgReferrers.DataSource = KC.DataAccess.MV6.utils.GetRecentReferrers();
		dgReferrers.DataBind();


	}
	protected void dgHits_PreRender(object sender, EventArgs e)
	{
		foreach (DataGridItem i in dgHits.Items)
		{
			i.Cells[0].Text = DateTime.Parse(i.Cells[0].Text).ToString("MMMM yyyy");

			int Total = int.Parse(i.Cells[1].Text),
				Spiders = int.Parse(i.Cells[2].Text),
				Humans = int.Parse(i.Cells[3].Text);

			i.Cells[1].Text = Total.ToString("###,###");
			i.Cells[2].Text = Spiders.ToString("###,###") + " (" + (((double)Spiders / (double)Total) * 100).ToString("###") + "%)";
			i.Cells[3].Text = Humans.ToString("###,###") + " (" + (((double)Humans / (double)Total) * 100).ToString("###") + "%)";
		}
	}
	protected void dgSearchStatus_ItemDataBound(object sender, DataGridItemEventArgs e)
	{
		if (sender == null || e == null) return; // not gonna happen
		if (e.Item.DataItem == null)
			return;
		e.Item.Cells[1].Text = e.Item.Cells[1].Text.ToLower();
		e.Item.Cells[1].Text = e.Item.Cells[1].Text[0].ToString().ToUpper() + e.Item.Cells[1].Text.Substring(1);
		e.Item.Cells[2].Text = e.Item.Cells[2].Text.ToLower();

	}
	protected void dgSearches_ItemDataBound(object sender, DataGridItemEventArgs e)
	{
		if (sender == null || e == null) return; // not gonna happen
		if (e.Item.DataItem == null)
			return;
		e.Item.Cells[0].Text = e.Item.Cells[0].Text.ToLower();
		e.Item.Cells[0].Text = e.Item.Cells[0].Text[0].ToString().ToUpper() + e.Item.Cells[0].Text.Substring(1);
	}
	protected void dgReferrers_ItemDataBound(object sender, DataGridItemEventArgs e)
	{
		if (sender == null || e == null) return; // not gonna happen
		string txt = e.Item.Cells[0].Text.Trim();
		if (txt.Length > 5 && txt[2] == '.')
			txt = txt.Substring(3);
		e.Item.Cells[0].Text = txt;
	}

	public bool isMyDomain(Activity act)
	{
		if (string.IsNullOrEmpty(act.Referrer)) return false;
		if (act == null) return false;
		return new Uri(act.Referrer).isMyDomain();
	}

}